package Service;

import java.sql.SQLException;

import java.util.ArrayList;
import java.util.List;

import dao.HotelDao;
import entity.City;
import entity.Country;
import entity.Hotel;
import entity.Hotel_label;
import entity.Hotel_reserve;
import entity.PageBean;
import entity.Payment;
import entity.ZC_user;

public class HotelService {
	private HotelDao hotelDao = new HotelDao();

	/**
	 * 查询所有国家名
	 * @return
	 * @throws SQLException 
	 */
	public List<Country> findCountry(){
		List<Country> lis = null;
		try {
			lis = hotelDao.findCoutry();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return lis;
	}
	/**
	 * 查询对应国家下所有的城市名
	 * @param country_id
	 * @return
	 */
	public List<City> findCity(int country_id) {
		List<City> lis = null;
		try {
			lis = hotelDao.findCity(country_id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return lis;
	}
	
	/**
	 * 根据id查询单个国家名称
	 * @param country_id
	 * @return
	 */
	public Country findCountryName(int country_id) {
		Country country = null;
		try {
			country=hotelDao.findCountryName(country_id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return country;
	}
	
	/**
	 * 根据id查找单个城市的名称
	 * @param city_id
	 * @return
	 */
	public List<City> findCityIDByCountryId(int country_id) {
		List<City> city = null;
		try {
			city = hotelDao.findCityIDByCountryId(country_id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return city;
	}
	
	/**
	 * 查询所有商品的信息
	 * @return 商品的信息
	 */
	public List<Hotel> findAllStore() {
		List<Hotel> hotel = null;
		try {
			hotel = hotelDao.findAllStore();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return hotel;
	}
	
	/**
	 * 根据商品的id
	 * 查询该商品的所有label
	 * @param hotel_id
	 * @return
	 */
	public List<Hotel_label> findHotelLabel(int hotel_id) {
		List<Hotel_label> lis = null;
		try {
			lis = hotelDao.findHotelLabel(hotel_id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return lis;
	}
	
	/**
	 * 单个城市下的商品分页展示
	 * @param city_id
	 * @param currentPage
	 * @param pageSize
	 * @return
	 */
	public PageBean<Hotel> pageQuery(int city_id, int currentPage, int pageSize) {
		// 1、封装PageBean
		PageBean<Hotel> pb = new PageBean<Hotel>();
		// 当前页
		pb.setCurrentPage(currentPage);
		// 每页条数
		pb.setPageSize(pageSize);
		// 设置总得记录数
		int totalCount = hotelDao.findTotalCount(city_id);
		pb.setTotalCount(totalCount);
		
		int start= (currentPage -1)*pageSize;
		List<Hotel> list = hotelDao.findByPage(city_id, start, pageSize);
		// 当前页显示的数据集合
		pb.setList(list );
		
		// 设置总页数
		int totalPage = totalCount % pageSize == 0?totalCount / pageSize :(totalCount / pageSize)+1;
		pb.setTotalPage(totalPage);
		return pb;
	}
	/**
	 * 全部的商品分页展示
	 * @param currentPage 当前页
	 * @param pageSize 一页的数量
	 * @return PageBean <Hotel>
	 */
	public PageBean<Hotel> allPageQuery(int currentPage, int pageSize) {
		// 1、封装PageBean
		PageBean<Hotel> pb = new PageBean<Hotel>();
		// 当前页
		pb.setCurrentPage(currentPage);
		// 每页条数
		pb.setPageSize(pageSize);
		// 设置总得记录数
		int totalCount = hotelDao.findAllTotalCount();
		pb.setTotalCount(totalCount);
		
		int start= (currentPage -1)*pageSize;
		List<Hotel> list = hotelDao.findByAllPages( start, pageSize);
		// 当前页显示的数据集合
		pb.setList(list );
		
		// 设置总页数
		int totalPage = totalCount % pageSize == 0?totalCount / pageSize :(totalCount / pageSize)+1;
		pb.setTotalPage(totalPage);
		return pb;
	}
	
	/**
	 * 
	 * @param currentPage 当前页
	 * @param pageSize 每页条数
	 * @param integer city_id
	 * @return
	 */
	public PageBean<Hotel> allPagesByAllCityId(int currentPage,int pageSize,List<City> city){
		// 1、封装PageBean
		PageBean<Hotel> pb = new PageBean<Hotel>();
		// 当前页
		pb.setCurrentPage(currentPage);
		// 每页条数
		pb.setPageSize(pageSize);
		// 设置总得记录数
		List<Hotel> lists =new ArrayList<Hotel>();
		for (City cid : city) {
			// 1.2 得到城市List
			List<Hotel> list1 =findStoreByCityId(cid.getCity_id());
			lists.addAll(list1);
		}
		int totalCount = lists.size();
		pb.setTotalCount(totalCount);
		
		// 定义从哪开始查
		int start= (currentPage -1)*pageSize;
		List<Hotel> list = new ArrayList<Hotel>();
		// 得到指定位置的List<List<Hotel>>的值
		for (int i = start; i < (start+pageSize); ) {
			// 做个最大值的判定
			if (i < lists.size() && lists.size()!=0) {
				// 将值添加到集合中
				list.add(lists.get(i));
				i++;
			}else {
				break;
			}
		}
		// 当前页显示的数据集合
		pb.setList(list);
		
		// 设置总页数
		int totalPage = totalCount % pageSize == 0?totalCount / pageSize :(totalCount / pageSize)+1;
		pb.setTotalPage(totalPage);
		return pb;
	}
	
	
	/**
	 * 查找该城市下的所有酒店
	 * @param city_id 城市的id
	 * @return
	 */
	public List<Hotel> findStoreByCityId(int city_id) {
		List<Hotel> hotel = null;
		try {
			hotel = hotelDao.findStoreByCityId(city_id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return hotel;
	}
	/**
	 * 查询8个产品-新鲜甩货
	 * @return 
	 */
	public List<Hotel> getFreshProduct(int city_id) {
		List<Hotel> hotel = null;
		hotel = hotelDao.getFreshProduct(city_id);
		return hotel;
	}
	/**
	 * 根据城市名称最后得到酒店信息
	 * @param city_name城市名称
	 * @throws SQLException 
	 */
	public PageBean<Hotel> getCityIdByCityName(String city_name,int currentPage,int pageSize){
		
		// ①得到city类
		City city = null;
			try {
				city = hotelDao.getCityIdByCityName(city_name);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		if (city==null) {
			return null;
		}else {
			// ②分页展示---城市下的hotel
			PageBean<Hotel> hotels =  pageQuery(city.getCity_id(), currentPage, pageSize);
			return hotels;
		}
	}
	
	
	/**
	 * 
	 * @param hotel_id
	 * @return Hotel对象
	 * @throws SQLException 
	 */
	public Hotel findHotelByHotelId(int hotel_id){
		return hotelDao.findHotelByHotelId(hotel_id);
	}
	public Hotel_reserve findTypeByHotelId(int hotel_id) throws SQLException {
		return hotelDao.findTypeByHotelId(hotel_id);
	}
	
	public int addSeeNumber(int hotel_id) {
		Hotel_reserve h_reserve = hotelDao.querySeeNumber(hotel_id);
		return hotelDao.addSeeNumber(h_reserve.getSee_number(),hotel_id);
	}
	public int queryOrders(String number) {
		return hotelDao.querOrders(number);
	}
	public ZC_user queryZcUsername(int user_id) {
		return hotelDao.queryZcUsername(user_id);
	}
	public ZC_user queryZcUserid(String user_Name) {
		
		return hotelDao.queryZcUserid(user_Name);
	}
	public Payment queryPay(String number) {
		System.out.println("service..........."+number);
		return hotelDao.queryPay(number);
	}
	// 销售出去
	public int updateHotel(int hotel_id,int sale_number,int save_number) {
		sale_number++;
		save_number--;
		return hotelDao.updateHotel(hotel_id,sale_number,save_number);
	}
	public Hotel querHotel(int hotel_id) {
		return hotelDao.querHotel(hotel_id);
	}
	
}
